import { request } from '@/utils'
import type { Params } from '@/types'
import qs from 'qs'

// types
interface User {
  name: string
  email: string
}
interface LoginParams {
  account: string
  password: string
}
interface AuthResponseType {
  access_token: string
  expires_in: number
  refresh_token: string
  scope: string
  token_type: string
}

// functions
export async function login({
  account,
  password
}: LoginParams): Promise<AuthResponseType> {
  const params = {
    username: account,
    password,
    grant_type: 'password',
    scope: 'web',
    client_id: 'web',
    client_secret: 'web'
  }

  return request.request({
    method: 'post',
    url: '/auth/oauth/token',
    data: qs.stringify(params)
  })
}

export async function queryUser(id: string, params: Params): Promise<User> {
  return request.query('/user/' + id, params)
}
